package system.company.JSP.TagClass;

import java.io.IOException;
import java.io.Serializable;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class mydate extends TagSupport implements Serializable {

	protected static final String DATE_FMT = "date";
	protected static final String TIME_FMT = "time";
	protected static final String FULL_FMT = "full";
	private String format=FULL_FMT;
	
	
	public String getFormat() {
		return format;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	public mydate() {
		// TODO Auto-generated constructor stub
	}
	public int doStartTag() throws JspException {
		try {
			String date = getDate(); 
                        //Output Writer
			JspWriter out = pageContext.getOut();
			out.print(date);
		} catch (IOException e) {
			System.out.println("Unexpected error in doStartTag()");
			e.printStackTrace();
		}
                
		return SKIP_BODY;
	}
	private String getDate() {
		Locale local = pageContext.getRequest().getLocale();
		String date = "";
		Date now = new Date();
		if (format.equals(DATE_FMT)) {
			DateFormat dateFmt = DateFormat.getDateInstance(DateFormat.FULL, local);
			date = dateFmt.format(now);
		} else if (format.equals(TIME_FMT)) {
			DateFormat timeFmt = DateFormat.getTimeInstance(DateFormat.FULL, local);
			date = timeFmt.format(now);
		} else if (format.equals(FULL_FMT)) {
			DateFormat dateFmt = DateFormat.getDateInstance(DateFormat.FULL, local);
			DateFormat timeFmt = DateFormat.getTimeInstance(DateFormat.FULL, local);
			date = dateFmt.format(now) + " " + timeFmt.format(now);
		}
		return date;
	}

}
